package mobidever.godutch.database.sqlitedal;

import java.util.Date;
import java.util.List;

import mobidever.godutch.activity.R;
import mobidever.godutch.database.base.SQLiteDALBase;
import mobidever.godutch.model.ModelAccountBook;
import mobidever.godutch.utility.DateTools;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class SQLiteDALAccountBook extends SQLiteDALBase {

	public SQLiteDALAccountBook(Context pContext) {
		super(pContext);
	}

	public boolean InsertAccountBook(ModelAccountBook pModelAccountBook) {
		ContentValues _ContentValues = CreateParms(pModelAccountBook);
		long _newID = getDatabase().insert(getTableNameAndPK()[0], null,
				_ContentValues);
		pModelAccountBook.setmAccountBookID((int) _newID);
		return _newID > 0;
	}

	public boolean DeleteAccountBook(String pCondition) {
		return Delete(getTableNameAndPK()[0], pCondition);
	}

	public boolean UpdateAccountBook(String pCondition, ModelAccountBook pModelAccountBook) {
		ContentValues _ContentValues = CreateParms(pModelAccountBook);
		return getDatabase().update(getTableNameAndPK()[0], _ContentValues,
				pCondition, null) > 0;
	}

	public boolean UpdateAccountBook(String pCondition, ContentValues pContentValues) {
		return getDatabase().update("AccountBook", pContentValues, pCondition, null)>0;
	}

	public List<ModelAccountBook> getAccountBooks(String pCondition) {
		String _SqlText = "Select * From AccountBook Where 1=1 and " + pCondition;
		List<ModelAccountBook> AccountBookList = getList(_SqlText);
		return AccountBookList;
	}

	@Override
	protected String[] getTableNameAndPK() {

		return new String[] { "AccountBook", "AccountBookID" };
	}

	@Override
	protected Object FindModel(Cursor pCursor) {
		ModelAccountBook _ModelAccountBook = new ModelAccountBook();
		_ModelAccountBook.setmAccountBookID(pCursor.getInt(pCursor.getColumnIndex("AccountBookID")));
		_ModelAccountBook.setmAccountBookName(pCursor.getString(pCursor
				.getColumnIndex("AccountBookName")));
		Date _CreateDate = DateTools.getDate(
				pCursor.getString(pCursor.getColumnIndex("CreateDate")),
				"yyyy-MM-dd HH:mm:ss");
		_ModelAccountBook.setmCreateDate(_CreateDate);
		_ModelAccountBook.setmState(pCursor.getInt(pCursor.getColumnIndex("State")));
		_ModelAccountBook.setmIsDefault(pCursor.getInt(pCursor.getColumnIndex("IsDefault")));
		return _ModelAccountBook;
	}

	private void InitDefaultData(SQLiteDatabase pDatabase) {
		String _AccountBook[] = getContext().getResources().getStringArray(R.array.InitDefaultAccountBookName);
		ModelAccountBook _ModelAccountBook = new ModelAccountBook();
		_ModelAccountBook.setmAccountBookName(_AccountBook[0]);
		_ModelAccountBook.setmIsDefault(1);
		ContentValues _ContentValues = CreateParms(_ModelAccountBook);
		pDatabase.insert("AccountBook", null, _ContentValues);
	}

	@Override
	public void OnCreate(SQLiteDatabase pDataBase) {
		StringBuilder sCreateTableScript = new StringBuilder();
		sCreateTableScript.append("Create TABLE AccountBook(");
		sCreateTableScript
				.append("AccountBookID integer PRIMARY KEY AUTOINCREMENT NOT NULL");
		sCreateTableScript.append(",AccountBookName varchar(10) NOT NULL");
		sCreateTableScript.append(",CreateDate datetime NOT NULL");
		sCreateTableScript.append(",State integer NOT NULL");
		sCreateTableScript.append(",IsDefault integer NOT NULL");
		sCreateTableScript.append(")");
		pDataBase.execSQL(sCreateTableScript.toString());
		Log.i("mylog","Create AccountBOok db");
		InitDefaultData(pDataBase);
	}

	@Override
	public void OnUpgrade(SQLiteDatabase pDataBase) {
		// TODO Auto-generated method stub

	}

	public ContentValues CreateParms(ModelAccountBook pInfo) {
		ContentValues _ContentValues = new ContentValues();
		_ContentValues.put("AccountBookName", pInfo.getmAccountBookName());
		_ContentValues.put("CreateDate", DateTools.getFormatDateTime(
				pInfo.getmCreateDate(), "yyyy-MM-dd HH:mm:ss"));
		_ContentValues.put("State", pInfo.getmState());
		_ContentValues.put("IsDefault", pInfo.getmIsDefault());
		return _ContentValues;
	}
}
